package com.fivestar.finance.app.ui.iou;


import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.fivestar.finance.app.R;
import com.fivestar.finance.app.ui.BaseFragment;
import com.fivestar.finance.app.ui.main.MainComponent;

import butterknife.BindView;

/**
 * A simple {@link Fragment} subclass.
 */
public class IouFragment extends BaseFragment {


    @BindView(R.id.webview_pb)
    ProgressBar webviewPb;

    @BindView(R.id.webview)
    WebView webview;


    private static final int PROGRESS_RATIO = 1000;


    @Override
    public void initInjector() {
        getComponent(MainComponent.class).inject(this);
    }

    @Override
    public int initContentView() {
        return R.layout.fragment_iou;
    }

    @Override
    public void getBundle(Bundle bundle) {

    }

    @Override
    public void initUI(View view) {
        this.enableJavascript();
        this.enableCaching();
        this.enableCustomClients();
        this.enableAdjust();
        this.zoomedOut();
        this.webview.loadUrl("http://vczero.github.io/search/html/index.html");

        getActivity().setTitle("http://vczero.github.io/search/html/index.html");
        showContent(true);
    }

    @Override
    public void initData() {

    }


    private void enableCustomClients() {
        this.webview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }


            /**
             * @param view The WebView that is initiating the callback.
             * @param url  The url of the page.
             */
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);

//                if (url.contains("www.vmovier.com")) {
//                    WebViewUtils.injectCSS(getActivity(),
//                            IouFragment.this.webview, "vmovier.css");
//                } else if (url.contains("video.weibo.com")) {
//                    WebViewUtils.injectCSS(getActivity(),
//                            IouFragment.this.webview, "weibo.css");
//                } else if (url.contains("m.miaopai.com")) {
//                    WebViewUtils.injectCSS(getActivity(),
//                            IouFragment.this.webview, "miaopai.css");
//                }
            }
        });
        this.webview.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                webviewPb.setProgress(progress);
                ((AppCompatActivity) getActivity()).setProgress(progress * PROGRESS_RATIO);
                if (progress >= 90) {
                    webviewPb.setVisibility(View.GONE);
                    webview.setVisibility(View.VISIBLE);
                } else {
                    webviewPb.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    @SuppressLint("SetJavaScriptEnabled")
    private void enableJavascript() {
        this.webview.getSettings().setJavaScriptEnabled(true);
        this.webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    }

    private void enableCaching() {
        this.webview.getSettings().setAppCachePath(getActivity().getFilesDir() + getActivity().getPackageName() + "/cache");
        this.webview.getSettings().setAppCacheEnabled(true);
        this.webview.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
    }


    private void enableAdjust() {
        this.webview.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        this.webview.getSettings().setLoadWithOverviewMode(true);
    }


    private void zoomedOut() {
        this.webview.getSettings().setLoadWithOverviewMode(true);
        this.webview.getSettings().setUseWideViewPort(true);
        this.webview.getSettings().setSupportZoom(true);
    }


}
